package org.eiahe.hr.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eiahe.hr.attendance.domain.AttendanceType;
import org.eiahe.hr.attendance.domain.bo.AttendanceTypeBo;
import org.eiahe.hr.attendance.domain.vo.AttendanceTypeVo;
import org.eiahe.hr.attendance.mapper.AttendanceTypeMapper;
import org.eiahe.hr.attendance.service.IAttendanceTypeService;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 考勤人员类型Service业务层处理
 *
 * @author zhangnn
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AttendanceTypeServiceImpl implements IAttendanceTypeService {

    private final AttendanceTypeMapper baseMapper;

    /**
     * 查询考勤人员类型
     */
    @Override
    public AttendanceTypeVo queryById(Long typeId) {
        return baseMapper.selectVoById(typeId);
    }

    /**
     * 查询考勤人员类型列表
     */
    @Override
    public TableDataInfo<AttendanceTypeVo> queryPageList(AttendanceTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AttendanceType> lqw = buildQueryWrapper(bo);
        Page<AttendanceTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询考勤人员类型列表
     */
    @Override
    public List<AttendanceTypeVo> queryList(AttendanceTypeBo bo) {
        LambdaQueryWrapper<AttendanceType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AttendanceType> buildQueryWrapper(AttendanceTypeBo bo) {
        LambdaQueryWrapper<AttendanceType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTypeName()), AttendanceType::getTypeName, bo.getTypeName());
        lqw.eq(StringUtils.isNotBlank(bo.getNeedPunch()), AttendanceType::getNeedPunch, bo.getNeedPunch());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AttendanceType::getStatus, bo.getStatus());
        lqw.orderByDesc(AttendanceType::getCreateTime);
        return lqw;
    }

    /**
     * 新增考勤人员类型
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(AttendanceTypeBo bo) {
        AttendanceType add = MapstructUtils.convert(bo, AttendanceType.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTypeId(add.getTypeId());
        }
        return flag;
    }

    /**
     * 修改考勤人员类型
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(AttendanceTypeBo bo) {
        AttendanceType update = MapstructUtils.convert(bo, AttendanceType.class);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 校验并批量删除考勤人员类型信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return baseMapper.deleteByIds(ids) > 0;
    }
}
